package com.chen.dataGet;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import com.chen.entity.Book;
import com.chen.utils.SortUtil;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

	/**
	* @author chen
	* @date ${2016-10-30}
	* @version 1.0
	*/

public class Writer_excel {
	
	static int count = 1;
	static int count2 = 1;

	public static void writerExcel(){

		try{
			//标题行
			String title[] = {"序号","书名","评分","评价人数","作者","出版社","出版日期","价格"};
			
			WritableWorkbook book = Workbook.createWorkbook(new File("D://数据爬虫.xls"));
			
			WritableSheet sheet=book.createSheet("第一页",0);
			
			//写入数据
            for(int i=0;i<8;i++)    //title
                sheet.addCell(new Label(i,0,title[i]));
            
            Set<String> set = DataGet.map.keySet(); 
            Object[] str = set.toArray();

            for(int i=0;i<str.length;i++){
			String message[] = {
					String.valueOf(count),
					DataGet.map.get(str[count-1]).getBookName(),
					DataGet.map.get(str[count-1]).getScore(),
					String.valueOf(DataGet.map.get(str[count-1]).getNumber()),
					DataGet.map.get(str[count-1]).getAuthor(),
					DataGet.map.get(str[count-1]).getPress(),
					DataGet.map.get(str[count-1]).getDate(),
					DataGet.map.get(str[count-1]).getValue()
					};
			
			for(int j=0;j<8;j++){
				sheet.addCell(new Label(j,count,message[j]));
				} 
				count++;
            }

			book.write();
			book.close(); 
		}catch(Exception e){		
	}
  }
	

	@SuppressWarnings("unchecked")
	public static void writerExcelFirst100(){
		
		Map<Book,Double> mHB = new HashMap<Book,Double>();
		Set<String> sHB = DataGet.mapSort.keySet();
		Object[] strHB = sHB.toArray();
		for(int k=0;k<strHB.length;k++){
			mHB.put(DataGet.mapSort.get(strHB[k]), Double.valueOf(DataGet.mapSort.get(strHB[k]).getScore()));//按<book,评分>的K-V模式存入Map中
		}
		Map<Book,Double> mBD = SortUtil.sortByComparator(mHB);//调用排序方法进行排序
		
		try{
			//标题行
			String title[] = {"序号","书名","评分","评价人数","作者","出版社","出版日期","价格"};
			
			WritableWorkbook book = Workbook.createWorkbook(new File("D://评分最高前100.xls"));
			
			WritableSheet sheet=book.createSheet("第一页",0);
			
			//写入数据
            for(int i=0;i<8;i++)    //title
                sheet.addCell(new Label(i,0,title[i]));
            
            Set<Book> set = mBD.keySet();
            Object[] str = set.toArray();
            
            for(int i=0;i<100;i++){
            	String message[] = {
					String.valueOf(count2),
					((Book) str[i]).getBookName(),
					((Book) str[i]).getScore(),
					((Book) str[i]).getNumber(),
					((Book) str[i]).getAuthor(),
					((Book) str[i]).getPress(),
					((Book) str[i]).getDate(),
					((Book) str[i]).getValue()
					};
    			for(int j=0;j<8;j++){
    				sheet.addCell(new Label(j,count2,message[j]));
    				} 
    				count2++;	
            }
            
			book.write();
			book.close(); 
		}catch(Exception e){		
	}
  }
}
